package com.xiaoming.backend.controller;

import com.xiaoming.backend.dto.request.LoginRequest;
import com.xiaoming.backend.dto.request.RegisterRequest;
import com.xiaoming.backend.dto.response.TaskCountResponse;
import com.xiaoming.backend.dto.response.UserResponse;
import com.xiaoming.backend.entity.orm.User;
import com.xiaoming.backend.result.exception.AppErrorException;
import com.xiaoming.backend.result.exception.error.AppError;
import com.xiaoming.backend.service.IAuthService;
import com.xiaoming.backend.service.IWasteService;
import com.xiaoming.backend.util.EntityUtil;
import com.xiaoming.backend.util.JsonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
@PreAuthorize("@securityService.checkInstitution(#institutionId)")
@Tag(name = "AuthController", description = "用户相关操作接口")
public class AuthController {
    private final IAuthService authService;

    private final IWasteService wasteService;

    @PostMapping("/register")
    @Operation(summary = "register", description = "注册")
    public ResponseEntity<?> register(@RequestBody RegisterRequest request) {
        try {
            UserResponse user = authService.register(request);
            return ResponseEntity.ok(user);
        }catch (AppErrorException ae){
            return ResponseEntity.ok(ae);
        }
    }

    @PostMapping("/login")
    @Operation(summary = "login", description = "登录")
    public ResponseEntity<?> login(@RequestBody LoginRequest request) {
        try {
            return ResponseEntity.ok(authService.login(request));
        }catch (AppErrorException ae){
            return ResponseEntity.ok(ae);
        }
    }

    @GetMapping("/user")
    @Operation(summary = "user", description = "获取用户信息")
    public ResponseEntity<?> user() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            return ResponseEntity.ok(EntityUtil.userTOUserResponse(user));
        }catch (AppErrorException e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrorMsg());
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(AppError.TOKEN_ERROR.getErrorMsg());
        }
    }

    @GetMapping("/count")
    @Operation(summary = "getCount", description = "获取用户任务数量信息")
    public ResponseEntity<?> getCount() {
        String jsonStr = "";
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            TaskCountResponse countResponse = wasteService.getTaskCount(user);
            jsonStr = JsonUtil.objToJson(countResponse);
        }catch (AppErrorException e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getErrorMsg());
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.FORBIDDEN).body(AppError.TOKEN_ERROR.getErrorMsg());
        }
        return ResponseEntity.ok(jsonStr);
    }
}
